//
//  CADelBankCardViewController.m
//  LNH
//
//  Created by twentylx on 2018/2/8.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CADelBankCardViewController.h"
#import "CADelBankCardView.h"
#import "CADelBankCardApi.h"
#import "CABankCardViewController.h"

@interface CADelBankCardViewController ()

@property (nonatomic, strong) CADelBankCardView *delBankCardView;

@end

@implementation CADelBankCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 初始化数据
- (void)lm_initialDefaultsForController {
    
}

/// 绑定 vm
- (void)lm_bindViewModelForController {
    
}

/// 创建视图
- (void)lm_createViewForController {
    [self.view setNeedsUpdateConstraints];
    
    [self.view addSubview:self.delBankCardView];
}

/// 配置导航栏
- (void)lm_configNavigationForController {
    self.navigationItem.title = @"解除银行卡";
}

#pragma makr layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
    
    [_delBankCardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark get & set
- (CADelBankCardView *)delBankCardView {
    if (!_delBankCardView) {
        _delBankCardView = [CADelBankCardView new];
        _delBankCardView.cardLabel.text = self.cardNo;
        
        [self.delBankCardView.delButton addTarget:self action:@selector(delAtion) forControlEvents:UIControlEventTouchUpInside];
    }
    return _delBankCardView;
}

#pragma mark private method
- (void) delAtion {
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"确定解绑银行卡？" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
   
    UIAlertAction *delAction = [UIAlertAction actionWithTitle:@"确定解除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            CADelBankCardApi *delBankApi = [[CADelBankCardApi alloc] initWithCardId:self.cardId];
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            [delBankApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                CADelBankCardApi *api = (CADelBankCardApi *) request;
                if (api.done) {
                    NSString *router = [JLRoutes lm_generateURLWithPattern:LMNavPushRoute parameters:@[NSStringFromClass(CABankCardViewController.class)] extraParameters:nil];
                    [[RACScheduler mainThreadScheduler] schedule:^{
        
                        [[UIApplication sharedApplication] openURL:JLRGenRouteURL(LMDefaultRouteSchema, router) options:nil completionHandler:nil];
        
                    }];
                } else {
                    [SVProgressHUD lm_displayErrorWithStatus:api.errorMsg];
                }
        
            } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
                [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
                [MBProgressHUD hideHUDForView:self.view animated:YES];
            }];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertC addAction:delAction];
    [alertC addAction:cancelAction];
    [self.navigationController presentViewController:alertC animated:YES completion:nil];

}

@end


